package com.um.jdy.app.charge.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.alipay.api.AlipayApiException;
import com.um.jdy.app.charge.entity.dto.EVPayParam;
import com.um.jdy.app.charge.entity.dto.EVStartParam;
import com.um.jdy.app.charge.entity.dto.EVStopParam;
import com.um.jdy.app.charge.manager.EVOrderManager;
import com.um.jdy.app.charge.manager.OrderManager;
import com.um.springboot.starter.annotation.ActionLogRequired;
import com.um.springboot.starter.orm.entity.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


@RestController
@RequestMapping("/api/v1/order/charge/ev")
@Api(tags = "充电订单-汽车")
@Validated
public class EVOrderController {

    @Autowired
    OrderManager orderManager;

    @Autowired
    EVOrderManager evOrderManager;

    @SaCheckLogin
    @ApiOperation("开始充电")
    @PostMapping("start")
    public Response start(@RequestBody @Valid EVStartParam param) {
        return Response.success(evOrderManager.startCharge(param));
    }

    @SaCheckLogin
    @ApiOperation("结束充电")
    @PostMapping("stop")
    public Response stop(@RequestBody @Valid EVStopParam param) throws Exception {
        evOrderManager.stopCharge(param.getOrderNo());
        return Response.success();
    }

    @SaCheckLogin
    @ApiOperation("支付")
    @PostMapping("pay")
    public Response Pay(@RequestBody @Valid EVPayParam param) {
        return Response.success(evOrderManager.pay(param));
    }


    /**
     * 微信支付回调
     * @param request
     * @param com_id
     * @return
     */
    @ActionLogRequired
    @RequestMapping("wxpay/notify/{com_id}")
    public Response wxPayNotify(HttpServletRequest request, @PathVariable String com_id) {
        evOrderManager.wxPayNotify(request, com_id);
        return Response.success();
    }

    @ActionLogRequired
    @RequestMapping(value = "alipay/notify")
    @ResponseBody
    public String alipayNotify(HttpServletRequest request) throws AlipayApiException {
        evOrderManager.aliPayNotify(request);
        return "success";
    }

}
